package android.rockchip.update.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.rockchip.update.service.R;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Class  Name: CommonDialog
 * Description:
 * Created by bruce on 17/11/7
 */

public class CommonDialog extends Dialog {

    private CommonDialog(Context context, int theme) {
        super(context, theme);
    }

    public void showWithOutTouchable(boolean touchable) {
        this.setCanceledOnTouchOutside(touchable);
        this.show();
    }

    /**
     * builder class for creating a custom dialog
     */
    public static class Builder {
        private Context context;
        private String title;
        private String message; // 对话框内容
        private String subMessage; // 对话框内容
        private String backBtnText; // 对话框返回按钮文本
        private String confirmBtnText; // 对话框确定文本

        // 对话框按钮监听事件
        private OnClickListener cancelButtonClickListener, confirmButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置title显示，resource获取
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 设置title显示，直接设置字符串
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 使用字符串设置对话框消息
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 使用资源设置对话框消息
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * 使用资源设置对话框消息
         */
        public Builder setSubMessage(String subMessage) {
            this.subMessage = subMessage;
            return this;
        }

        /**
         * 设置自定义的对话框内容
         */
        public Builder setContentView(View v) {
            return this;
        }

        /**
         * 设置back按钮的事件和文本（resource资源）
         */
        public Builder setCancelButton(int text, OnClickListener listener) {
            this.backBtnText = (String) context.getText(text);
            this.cancelButtonClickListener = listener;
            return this;
        }

        /**
         * 设置back按钮的事件和文本（字符串）
         */
        public Builder setCancelButton(String backButtonText, OnClickListener listener) {
            this.backBtnText = backButtonText;
            this.cancelButtonClickListener = listener;
            return this;
        }

        /**
         * 设置确定按钮事件和文本（resource资源）
         */
        public Builder setConfirmButton(int text, OnClickListener listener) {
            this.confirmBtnText = (String) context.getText(text);
            this.confirmButtonClickListener = listener;
            return this;
        }

        /**
         * 设置确定按钮事件和文本（字符串）
         */
        public Builder setConfirmButton(String confirmButtonText, OnClickListener listener) {
            this.confirmBtnText = confirmButtonText;
            this.confirmButtonClickListener = listener;
            return this;
        }

        /**
         * 创建自定义的对话框
         */
        public CommonDialog create() {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // 实例化自定义的对话框主题
            final CommonDialog dialog = new CommonDialog(context, R.style.Dialog);

            View layout = inflater.inflate(R.layout.dialog_common_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            TextView tvTitle = (TextView) layout.findViewById(R.id.dialog_title);
            if (TextUtils.isEmpty(title)) {
                tvTitle.setVisibility(View.GONE);
            } else {
                tvTitle.setText(title);
            }

            TextView tvMessage = (TextView) layout.findViewById(R.id.dialog_message);
            if (TextUtils.isEmpty(message)) {
                tvMessage.setVisibility(View.GONE);
            } else {
                tvMessage.setText(message);
            }

            TextView tvSubMessage = (TextView) layout.findViewById(R.id.dialog_sub_message);
            if (TextUtils.isEmpty(subMessage)) {
                tvSubMessage.setVisibility(View.GONE);
            } else {
                tvSubMessage.setText(subMessage);
            }
            // 设置返回按钮事件和文本
            if (backBtnText != null) {
                Button bckButton = ((Button) layout.findViewById(R.id.dialog_back));
                bckButton.setText(backBtnText);

                if (cancelButtonClickListener != null) {
                    bckButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            cancelButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.dialog_back).setVisibility(View.GONE);
                layout.findViewById(R.id.divider_btn).setVisibility(View.GONE);
            }

            // 设置确定按钮事件和文本
            if (confirmBtnText != null) {
                Button cfmButton = ((Button) layout.findViewById(R.id.dialog_confirm));
                cfmButton.setText(confirmBtnText);

                if (confirmButtonClickListener != null) {
                    cfmButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            confirmButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.dialog_confirm).setVisibility(View.GONE);
                layout.findViewById(R.id.divider_btn).setVisibility(View.GONE);
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }

}
